package cn.edu.scau.cmi.chenyipeng.hibernate.tool;

import java.net.URL;
import java.util.ResourceBundle;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.chenyipeng.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.chenyipeng.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.chenyipeng.hibernate.domain.Student;
import cn.edu.scau.cmi.chenyipeng.hibernate.domain.Teacher;
import cn.edu.scau.cmi.chenyipeng.spring.util.ComprehensionApplicationContextUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.event.*;

public class HibernateCon {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button deleteSB;

    @FXML
    private Button deleteTB;

    @FXML
    private TextField sName;

    @FXML
    private Button sChooseTB;

    @FXML
    private Button sAlterTB;

    @FXML
    private Button addTB;

    @FXML
    private Button tAlterSB;

    @FXML
    private TextField tID;

    @FXML
    private TextField sID;

    @FXML
    private Button tChooseSB;

    @FXML
    private Button addSB;

    @FXML
    private TextField tName;

    @FXML
    private TextField sTeacher;

    @FXML
    private Button alterTB;

    @FXML
    private Button alterSB;

    @FXML
    void addS(ActionEvent event) {
    		Student s = new Student();
    		s.setId(Long.valueOf(sID.getText()));
    		s.setName(sName.getText());
    		StudentDAO sDAO = new StudentDAO();
    		/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
    		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDao");*/
    		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
    		Transaction t = session.beginTransaction();
    		sDAO.save(s);
    		t.commit();
    		session.close();
    }

    @FXML
    void alterS(ActionEvent event) {
    	Student s = new Student();
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		StudentDAO sDAO = new StudentDAO();
		/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDao");*/
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
    }

    @FXML
    void deleteS(ActionEvent event) {
    	Student s = new Student();
		s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		StudentDAO sDAO = new StudentDAO();
		/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDao");*/
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.delete(s);
		t.commit();
		session.close();
    }

    @FXML
    void sChooseT(ActionEvent event) {
    	Student s = new Student();
    	Teacher tea = new Teacher();
    	StudentDAO sDAO = new StudentDAO();
    	/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDao");*/
    	s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
    }

    @FXML
    void sAlterT(ActionEvent event) {
    	Student s = new Student();
    	Teacher tea = new Teacher();
    	StudentDAO sDAO = new StudentDAO();
    	/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDao");*/
    	s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
    }

    @FXML
    void addT(ActionEvent event) {
    	Teacher tea = new Teacher();
    	tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		TeacherDAO tDAO = new TeacherDAO();
		/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		TeacherDAO tDAO = (TeacherDAO) ac.getBean("teacherDao");*/
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		tDAO.save(tea);
		t.commit();
		session.close();
    }

    @FXML
    void alterT(ActionEvent event) {
    	Teacher tea = new Teacher();
    	tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		TeacherDAO tDAO = new TeacherDAO();
		/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		TeacherDAO tDAO = (TeacherDAO) ac.getBean("teacherDao");*/
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		tDAO.update(tea);
		t.commit();
		session.close();
    }

    @FXML
    void deleteT(ActionEvent event) {
    	Teacher tea = new Teacher();
    	tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		TeacherDAO tDAO = new TeacherDAO();
		/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		TeacherDAO tDAO = (TeacherDAO) ac.getBean("teacherDao");*/
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		tDAO.delete(tea);
		t.commit();
		session.close();
    }

    @FXML
    void tChooseS(ActionEvent event) {
    	Student s = new Student();
    	Teacher tea = new Teacher();
    	StudentDAO sDAO = new StudentDAO();
    	/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDao");*/
    	s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
    }

    @FXML
    void tAlterS(ActionEvent event) {
    	Student s = new Student();
    	Teacher tea = new Teacher();
    	StudentDAO sDAO = new StudentDAO();
    	/*ApplicationContext ac = ComprehensionApplicationContextUtil.getApplicationContext();
		StudentDAO sDAO = (StudentDAO) ac.getBean("studentDao");*/
    	s.setId(Long.valueOf(sID.getText()));
		s.setName(sName.getText());
		tea.setId(Long.valueOf(tID.getText()));
		tea.setName(tName.getText());
		tea.setStudent(s);
		s.setTeacher(tea);
		Session session = ScauCmiHibernateSessionFactoryUtil.getSession();
		Transaction t = session.beginTransaction();
		sDAO.update(s);
		t.commit();
		session.close();
    }

    @FXML
    void initialize() {
        assert deleteSB != null : "fx:id=\"deleteSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert deleteTB != null : "fx:id=\"deleteTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert sName != null : "fx:id=\"sName\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert sChooseTB != null : "fx:id=\"sChooseTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert sAlterTB != null : "fx:id=\"sAlterTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert addTB != null : "fx:id=\"addTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert tAlterSB != null : "fx:id=\"tAlterSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert tID != null : "fx:id=\"tID\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert sID != null : "fx:id=\"sID\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert tChooseSB != null : "fx:id=\"tChooseSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert addSB != null : "fx:id=\"addSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert tName != null : "fx:id=\"tName\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert sTeacher != null : "fx:id=\"sTeacher\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert alterTB != null : "fx:id=\"alterTB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";
        assert alterSB != null : "fx:id=\"alterSB\" was not injected: check your FXML file 'HibernateGUI.fxml'.";

    }
}

